import java.util.Scanner;

public class Test3 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            int m = in.nextInt();
            in.nextLine();
            char[] str1 = (" " + in.nextLine()).toCharArray();
            char[] str2 = (" " + in.nextLine()).toCharArray();
            // dp[i][j]表示str1[0,i]之间的子序列和str2[0,j]之间的子序列的额最长公共子序列的长度
            // if str1[i] == str2[j] dp[i][j] = dp[i-1][j-1]+1
            // else dp[i][j] = Math.max(dp[i][j-1], dp[i-1][j])
            int[][] dp = new int[n+1][m+1];
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) {
                    if (str1[i] == str2[j]) {
                        dp[i][j] = dp[i-1][j-1] + 1;
                    } else {
                        dp[i][j] = Math.max(dp[i][j-1], dp[i-1][j]);
                    }
                }
            }
            System.out.println(dp[n][m]);
        }
    }
}
